package com.leinao.category.controller;

import com.alibaba.fastjson.JSON;
import com.leinao.category.dto.CategoryCount;
import com.leinao.category.dto.CategoryDto;
import com.leinao.category.dto.CategoryRelationDto;
import com.leinao.category.model.BatchCategoryRelation;
import com.leinao.category.model.CategoryRelation;
import com.leinao.category.service.CategoryService;
import com.leinao.commons.Response;
import com.leinao.constant.Constant;
import com.leinao.login.dto.LoginResultDto;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 *
 * @Description 算法、数据集分类控制层
 * @author lizi
 * @date 2019/03/06 下午5:15
 * @Version 1.0
 */
@RestController
@CrossOrigin
public class CategoryController {

	private static Logger logger = LoggerFactory.getLogger(CategoryController.class);

	@Autowired
	private CategoryService categoryService;

	@ApiOperation(value = "获取分类列表信息", notes = "获取分类列表信息")
	@GetMapping(value = "/category/getCategoryListByType")
	public Response<List<CategoryDto>> getCategoryListByType(@ApiParam(value = "分类类型", required = true) @RequestParam Integer type) {
		logger.info("getCategoryListByType param  type: {}", type);

		return categoryService.getCategoryListByType(type);
	}

    @ApiOperation(value = "获取分类树信息", notes = "获取分类树信息")
    @GetMapping(value = "/category/getCategoryTreeByType")
    public Response<List<CategoryDto>> getCategoryTreeByType(@ApiParam(value = "分类类型", required = true) @RequestParam Integer type) {
        logger.info("getCategoryTreeByType param  type: {}", type);

        return categoryService.getCategoryTreeByType(type);
    }

    @ApiOperation(value = "获取主体分类关联关系", notes = "获取主体分类关联关系")
    @GetMapping(value = "/category/getCategoryRelationList")
    public Response<List<CategoryRelationDto>> getCategoryRelationList(@ApiParam(value = "分类主体ID", required = true) @RequestParam Long objectId,
                                                                       @ApiParam(value = "分类类型", required = true) @RequestParam Integer type) {
        logger.info("getCategoryRelationList param  objectId: {}, type: {}", objectId, type);

        return categoryService.getCategoryRelationList(objectId, type);
    }

    @ApiOperation(value = "删除分类", notes = "删除分类")
    @PostMapping(value = "/category/deleteCategoryRelation")
    public Response<Void> deleteCategoryRelation(@ApiParam(value = "分类关联ID", required = true) @RequestParam Long categoryRelationId,
                                                 HttpServletRequest request) {
        logger.info("deleteCategoryRelation param  categoryRelationId: {}", categoryRelationId);

        // 获取登录用户信息
        String obj = (String) request.getSession().getAttribute(Constant.USER_SESSION_KEY);
        LoginResultDto userInfo = JSON.parseObject(obj, LoginResultDto.class);

        return categoryService.deleteCategoryRelation(categoryRelationId, userInfo);
    }

    @ApiOperation(value = "添加分类", notes = "添加分类")
    @PostMapping(value = "/category/addCategoryRelation")
    @ApiImplicitParam(name = "categoryRelation", value = "categoryRelation", required = true, dataType = "CategoryRelation")
    public Response<Long> addCategoryRelation(@RequestBody CategoryRelation categoryRelation, HttpServletRequest request) {
        logger.info("addCategoryRelation param  categoryRelation: {}", categoryRelation);

        // 获取登录用户信息
        String obj = (String) request.getSession().getAttribute(Constant.USER_SESSION_KEY);
        LoginResultDto userInfo = JSON.parseObject(obj, LoginResultDto.class);

        return categoryService.addCategoryRelation(categoryRelation, userInfo);
    }

    @ApiOperation(value = "批量保存分类", notes = "批量保存分类")
    @PostMapping(value = "/category/saveCategoryRelationList")
    @ApiImplicitParam(name = "batchCategoryRelation", value = "batchCategoryRelation", required = true, dataType = "BatchCategoryRelation")
    public Response<Void> saveCategoryRelationList(@RequestBody BatchCategoryRelation batchCategoryRelation, HttpServletRequest request) {
        logger.info("saveCategoryRelationList param  batchCategoryRelation: {}", batchCategoryRelation);

        // 获取登录用户信息
        String obj = (String) request.getSession().getAttribute(Constant.USER_SESSION_KEY);
        LoginResultDto userInfo = JSON.parseObject(obj, LoginResultDto.class);

        return categoryService.saveCategoryRelationList(batchCategoryRelation, userInfo);
    }

    @ApiOperation(value = "获取公开项目关联最多的5个标签", notes = "获取公开项目关联最多的5个标签")
    @PostMapping(value = "/category/getTopFiveRelationCategory")
    public Response<List<CategoryDto>> saveCategoryRelationList(
            @ApiParam(value = "查询类型", required = true) @RequestParam Integer type,
            HttpServletRequest request) {

        // 获取登录用户信息
        String obj = (String) request.getSession().getAttribute(Constant.USER_SESSION_KEY);
        LoginResultDto userInfo = JSON.parseObject(obj, LoginResultDto.class);

        return categoryService.getTopFiveRelationCategory(type);
    }

}
